package com.project.servlet;

import com.github.pagehelper.PageInfo;
import com.project.Util.UtfUtil;
import com.project.bean.ManBean;
import com.project.service.IManService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author lyl
 * description:查询员工servlet
 */
@Controller
public class FindAllServlet extends HttpServlet {
    @Autowired
    private IManService iManService;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UtfUtil.utfUtil(req, resp);
        //得到页码
        String pg = req.getParameter("pageNo");
        int pageNo = 1;
        if (pg != null) {
            pageNo = Integer.parseInt(pg);
        }
        PageInfo<ManBean> pageInfo = iManService.findAll(pageNo);
        List<ManBean> list = pageInfo.getList();
        int pages = pageInfo.getPages();

        //使用字符串进行数据拼接
        String table = "<table border='1' width='60%'>";
        table += "<thead><tr><th>姓名</th><th>生日</th><th>点击操作</th></tr></thead>";
        table += "<tbody>";
        for (ManBean manBean : list) {
            table += "<tr><td>" + manBean.getManName() + "</td><td>" + manBean.getBirthday() + "</td>" +
                    "<th><a style='margin:5px' href=del?manId=" + manBean.getManId() + ">删除<a>" +
                    "<a href=findOne?manId=" + manBean.getManId() + ">点击修改<a>"
                    + "</th>"
                    + "</tr>";
        }
        table += "</tbody>";
        table += "</table>";

        for (int i = 1; i <= pages; i++) {
            table += "<a style='margin:5px' href=findAll?pageNo=" + i + ">第" + i + "页" + "</a>";
        }
        PrintWriter ps = resp.getWriter();
        ps.println(table);
    }
}
